package freaks.terah.model;

import java.util.ArrayList;
import java.util.Random;

public class EnvironmentModel {
	private Planet[] planets = new Planet[3];
	private int planetNumber;
	
	private final static int PLANET_EDEN = 0;
	private final static int PLANET_ICE = 1;
	private final static int PLANET_DESERTED_HUMAN = 2;
	private final static int PLANET_MINERAL = 3;
	private final static int PLANET_ALIEN = 4;
	private final static int PLANET_DESOLATED = 5;
	private final static int PLANET_MYSTERY = 7;
	
	private final static int RESSOURCE_EVERYWHERE = 0;
	private final static int RESSOURCE_PLENTIFUL = 1;
	private final static int RESSOURCE_COMMON = 2;
	private final static int RESSOURCE_UNCOMMON = 3;
	private final static int RESSOURCE_RARE = 4;
	private final static int RESSOURCE_VERY_RARE = 5;
	
	private final static int STRENGTH_DEFENSELESS = 0;
	private final static int STRENGTH_WEAK = 1;
	private final static int STRENGTH_AVERAGE = 2;
	private final static int STRENGTH_GOOD = 3;
	private final static int STRENGTH_POWERFUL = 4;
	
	private final static int B_MISSION = 0;
	private final static int B_TARGET = 1;
	private final static int B_POWER = 2;
	private final static int B_DEFENSE = 3;
	private final static int B_BRAIN = 4;
	private final static int B_CARRY = 5;
	private final static int B_AVL_RES = 6;
	// TODO: Uses this somewhere?
	//private final static int B_MILITARY_SCORE = 7;
	private final static int B_RESPONSE_TYPE = 8;
	
	public void generatePlanetData(int turn) {
		planetNumber = 3;
		Random rand = new Random();
		if (turn != 1)
		{
			int number = rand.nextInt(10)+1;
			
			switch (number) {
			case 1:
				planetNumber = 0;
				break;
			case 2:
			case 3:
			case 4:
				planetNumber = 1;
				break;
			case 5:
			case 6:
			case 7:
			case 8:
				planetNumber = 2;
				break;
			case 9:
			case 10:
				break;
			}
		}
		
		for (int i = 0; i < planetNumber; i++)
		{
			int planetType = PLANET_EDEN;
			
            int number = rand.nextInt(10)+1;
			
			switch (number) {
			case 1:
			case 2:
				planetType = PLANET_EDEN;
				break;
			case 3:
			case 4:
				planetType = PLANET_ICE;
				break;
			case 5:
			case 6:
				planetType = PLANET_DESERTED_HUMAN;
				break;
			case 7:
			case 8:
				planetType = PLANET_MINERAL;
				break;
			case 9:
				planetType = PLANET_ALIEN;
				break;
			case 10:
				planetType = PLANET_DESOLATED;
				break;
			}
			
			planets[i] = new Planet(planetType, generatePlanetRessources(planetType, rand), generatePlanetStats(planetType, rand));
		}
		
		if (planetNumber < 3 && rand.nextInt(10)+1 == 10)
		{
			planets[planetNumber] = new Planet(PLANET_MYSTERY, generatePlanetRessources(PLANET_MYSTERY, rand), generatePlanetStats(PLANET_MYSTERY, rand));
			planetNumber++;
		}
	}
	
	private int[] generatePlanetRessources(int planetType, Random seed)
	{
		int[] ressources = {0,0,0,0,0,0,0,0,0};
		
		switch (planetType)
		{
		case PLANET_EDEN:
			ressources[0] = generateRessourceQuantity(RESSOURCE_EVERYWHERE, seed);
			ressources[1] = generateRessourceQuantity(RESSOURCE_EVERYWHERE, seed);
			ressources[2] = generateRessourceQuantity(RESSOURCE_UNCOMMON, seed);
			ressources[3] = generateRessourceQuantity(RESSOURCE_RARE, seed);
			ressources[4] = generateRessourceQuantity(RESSOURCE_VERY_RARE, seed);
			ressources[5] = generateRessourceQuantity(RESSOURCE_RARE, seed);
			break;
		case PLANET_ICE:
			ressources[0] = generateRessourceQuantity(RESSOURCE_RARE, seed);
			ressources[1] = generateRessourceQuantity(RESSOURCE_PLENTIFUL, seed);
			ressources[2] = generateRessourceQuantity(RESSOURCE_EVERYWHERE, seed);
			ressources[3] = generateRessourceQuantity(RESSOURCE_VERY_RARE, seed);
			ressources[5] = generateRessourceQuantity(RESSOURCE_RARE, seed);
			break;
		case PLANET_DESERTED_HUMAN:
			ressources[0] = generateRessourceQuantity(RESSOURCE_UNCOMMON, seed);
			ressources[1] = generateRessourceQuantity(RESSOURCE_RARE, seed);
			ressources[2] = generateRessourceQuantity(RESSOURCE_UNCOMMON, seed);
			ressources[3] = generateRessourceQuantity(RESSOURCE_EVERYWHERE, seed);
			ressources[4] = generateRessourceQuantity(RESSOURCE_PLENTIFUL, seed);
			ressources[7] = generateRessourceQuantity(RESSOURCE_PLENTIFUL, seed);
			break;
		case PLANET_MINERAL:
			ressources[0] = generateRessourceQuantity(RESSOURCE_VERY_RARE, seed);
			ressources[1] = generateRessourceQuantity(RESSOURCE_EVERYWHERE, seed);
			ressources[2] = generateRessourceQuantity(RESSOURCE_UNCOMMON, seed);
			ressources[3] = generateRessourceQuantity(RESSOURCE_VERY_RARE, seed);
			ressources[5] = generateRessourceQuantity(RESSOURCE_UNCOMMON, seed);
			break;
		case PLANET_ALIEN:
			ressources[0] = generateRessourceQuantity(RESSOURCE_UNCOMMON, seed);
			ressources[1] = generateRessourceQuantity(RESSOURCE_UNCOMMON, seed);
			ressources[2] = generateRessourceQuantity(RESSOURCE_COMMON, seed);
			ressources[3] = generateRessourceQuantity(RESSOURCE_COMMON, seed);
			ressources[4] = generateRessourceQuantity(RESSOURCE_COMMON, seed);
			ressources[5] = generateRessourceQuantity(RESSOURCE_COMMON, seed);
			break;
		case PLANET_DESOLATED:
			ressources[0] = generateRessourceQuantity(RESSOURCE_UNCOMMON, seed);
			ressources[1] = generateRessourceQuantity(RESSOURCE_UNCOMMON, seed);
			ressources[2] = generateRessourceQuantity(RESSOURCE_UNCOMMON, seed);
			ressources[3] = generateRessourceQuantity(RESSOURCE_UNCOMMON, seed);
			ressources[4] = generateRessourceQuantity(RESSOURCE_UNCOMMON, seed);
			ressources[5] = generateRessourceQuantity(RESSOURCE_RARE, seed);
			break;
		case PLANET_MYSTERY:
			ressources[0] = 3;
			ressources[1] = 3;
			ressources[2] = 3;
			ressources[3] = 3;
			ressources[4] = 3;
			ressources[5] = 3;
			break;
		}
		
		return ressources;
	}
	
	private int[] generatePlanetStats(int planetType, Random seed)
	{
		int[] stats = {0,0,0};
		
		switch (planetType)
		{
		case PLANET_EDEN:
			stats[0] = generateStatQuantity(STRENGTH_GOOD, seed);
			stats[1] = generateStatQuantity(STRENGTH_WEAK, seed);
			stats[2] = generateStatQuantity(STRENGTH_DEFENSELESS, seed);
			break;
		case PLANET_ICE:
			stats[0] = generateRessourceQuantity(STRENGTH_DEFENSELESS, seed);
			stats[1] = generateRessourceQuantity(STRENGTH_GOOD, seed);
			stats[2] = generateRessourceQuantity(STRENGTH_WEAK, seed);
			break;
		case PLANET_DESERTED_HUMAN:
			stats[0] = generateRessourceQuantity(STRENGTH_AVERAGE, seed);
			stats[1] = generateRessourceQuantity(STRENGTH_AVERAGE, seed);
			stats[2] = generateRessourceQuantity(STRENGTH_AVERAGE, seed);
			break;
		case PLANET_MINERAL:
			stats[0] = generateRessourceQuantity(STRENGTH_WEAK, seed);
			stats[1] = generateRessourceQuantity(STRENGTH_POWERFUL, seed);
			stats[2] = generateRessourceQuantity(STRENGTH_AVERAGE, seed);
			break;
		case PLANET_ALIEN:
			stats[0] = generateRessourceQuantity(STRENGTH_WEAK, seed);
			stats[1] = generateRessourceQuantity(STRENGTH_AVERAGE, seed);
			stats[2] = generateRessourceQuantity(STRENGTH_POWERFUL, seed);
			break;
		case PLANET_DESOLATED:
			stats[0] = generateRessourceQuantity(STRENGTH_DEFENSELESS, seed);
			stats[1] = generateRessourceQuantity(STRENGTH_DEFENSELESS, seed);
			stats[2] = generateRessourceQuantity(STRENGTH_DEFENSELESS, seed);
			break;
		case PLANET_MYSTERY:
			stats[0] = 3;
			stats[1] = 3;
			stats[2] = 3;
			break;
		}
		
		return stats;
	}
	
	private int generateStatQuantity(int distributionType, Random seed)
	{
		int quantity = 0;
		
		int value = (seed.nextInt(10)+1);
		
		switch (distributionType)
		{
		case STRENGTH_DEFENSELESS:
			switch (value)
			{
			case 1:
			case 2:
			case 3:
				quantity = 1;
				break;
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
			case 9:
			case 10:
				quantity = 0;
				break;
			}
			break;
		case STRENGTH_WEAK:
			switch (value)
			{
			case 1:
			case 2:
				quantity = 2;
				break;
			case 3:
			case 4:
			case 5:
			case 6:
				quantity = 1;
				break;
			case 7:
			case 8:
			case 9:
			case 10:
				quantity = 0;
				break;
			}
			break;
		case STRENGTH_AVERAGE:
			switch (value)
			{
			case 1:
				quantity = 3;
				break;
			case 2:
			case 3:
			case 4:
				quantity = 2;
				break;
			case 5:
			case 6:
			case 7:
			case 8:
			case 9:
				quantity = 1;
				break;
			case 10:
				quantity = 0;
				break;
			}
			break;
		case STRENGTH_GOOD:
			switch (value)
			{
			case 1:
			case 2:
				quantity = 3;
				break;
			case 3:
			case 4:
			case 5:
			case 6:
				quantity = 2;
				break;
			case 7:
			case 8:
			case 9:
			case 10:
				quantity = 1;
				break;
			}
			break;
		case STRENGTH_POWERFUL:
			switch (value)
			{
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
				quantity = 3;
				break;
			case 6:
			case 7:
			case 8:
			case 9:
				quantity = 2;
				break;
			case 10:
				quantity = 1;
				break;
			}
			break;
		}
		
		return quantity;
	}
	
	private int generateRessourceQuantity(int distributionType, Random seed)
	{
		int quantity = 0;
		
		int value = (seed.nextInt(10)+1);
		
		switch (distributionType)
		{
		case RESSOURCE_EVERYWHERE:
			switch (value)
			{
			case 1:
			case 2:
			case 3:
				quantity = 2;
				break;
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
			case 9:
			case 10:
				quantity = 3;
				break;
			}
			break;
		case RESSOURCE_PLENTIFUL:
			switch (value)
			{
			case 1:
			case 2:
				quantity = 1;
				break;
			case 3:
			case 4:
			case 5:
			case 6:
				quantity = 2;
				break;
			case 7:
			case 8:
			case 9:
			case 10:
				quantity = 3;
				break;
			}
			break;
		case RESSOURCE_COMMON:
			switch (value)
			{
			case 1:
				quantity = 0;
				break;
			case 2:
			case 3:
			case 4:
				quantity = 1;
				break;
			case 5:
			case 6:
			case 7:
			case 8:
			case 9:
				quantity = 2;
				break;
			case 10:
				quantity = 3;
				break;
			}
			break;
		case RESSOURCE_UNCOMMON:
			switch (value)
			{
			case 1:
			case 2:
				quantity = 0;
				break;
			case 3:
			case 4:
			case 5:
			case 6:
				quantity = 1;
				break;
			case 7:
			case 8:
			case 9:
			case 10:
				quantity = 2;
				break;
			}
			break;
		case RESSOURCE_RARE:
			switch (value)
			{
			case 1:
			case 2:
				quantity = 0;
				break;
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
			case 9:
				quantity = 1;
				break;
			case 10:
				quantity = 2;
				break;
			}
			break;
		case RESSOURCE_VERY_RARE:
			switch (value)
			{
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
				quantity = 0;
				break;
			case 9:
			case 10:
				quantity = 1;
				break;
			}
			break;
		}
		
		return quantity;
	}
	
	public Data[] getPlanetData() {
		Data[] data = new Data[1+planetNumber*10];
		
		data[0] = new Data(planetNumber);
		
		for (int i = 0; i < planetNumber; i++)
		{
			planets[i].getRessources();
			data[1+i*10] = new Data(planets[i].getType());
			data[2+i*10] = new Data(planets[i].getRessources()[0]);
			data[3+i*10] = new Data(planets[i].getRessources()[1]);
			data[4+i*10] = new Data(planets[i].getRessources()[2]);
			data[5+i*10] = new Data(planets[i].getRessources()[3]);
			data[6+i*10] = new Data(planets[i].getRessources()[4]);
			data[7+i*10] = new Data(planets[i].getRessources()[5]);
			data[8+i*10] = new Data(planets[i].getStats()[0]);
			data[9+i*10] = new Data(planets[i].getStats()[1]);
			data[10+i*10] = new Data(planets[i].getStats()[2]);
		}
		
		return data;
	}

	public void setPlanetData(Data[] planetData) {
		planetNumber = Integer.parseInt(planetData[0].getData());
		
		for (int i = 0; i < planetNumber; i++)
		{
			int[] ressources = {0,0,0,0,0,0,0,0,0};
			int[] stats = {0,0,0};
			ressources[0] = Integer.parseInt(planetData[2+i*10].getData());
			ressources[1] = Integer.parseInt(planetData[3+i*10].getData());
			ressources[2] = Integer.parseInt(planetData[4+i*10].getData());
			ressources[3] = Integer.parseInt(planetData[5+i*10].getData());
			ressources[4] = Integer.parseInt(planetData[6+i*10].getData());
			ressources[5] = Integer.parseInt(planetData[7+i*10].getData());
			
			stats[0] = Integer.parseInt(planetData[8+i*10].getData());
			stats[1] = Integer.parseInt(planetData[9+i*10].getData());
			stats[2] = Integer.parseInt(planetData[10+i*10].getData());
			
			planets[i] = new Planet(Integer.parseInt(planetData[1+i*10].getData()), ressources, stats);
		}
	}
	
	public ArrayList<Data[]> doServerStuff(ArrayList<Data[]> clientCodes, int turn)
	{
		int[][] planetLanding = new int[planetNumber > 0 ? planetNumber : 1][clientCodes.size()];
		int[][] userLanding = new int[clientCodes.size()][clientCodes.size()];
		for (int i = 0; i < planetNumber; i++)
			for (int j = 0; j < clientCodes.size(); j++)
				planetLanding[i][j] = -1;
		
		for (int i = 0; i < clientCodes.size(); i++)
			for (int j = 0; j < clientCodes.size(); j++)
				userLanding[i][j] = -1;
			
		ArrayList<Data[]> returnRessources = new ArrayList<Data[]>();
		ArrayList<int[]> newRessources = new ArrayList<int[]>();
		
		for (int player = 0; player < clientCodes.size(); player++)
		{
			newRessources.add(new int[]{0,0,0,0,0,0,0,0,0,0});
			Data [] playerData = clientCodes.get(player);
			if (Integer.parseInt(playerData[B_MISSION].getData()) == 1)
			{
				int p = Integer.parseInt(playerData[B_TARGET].getData());
				int k = 0;
				while (planetLanding[p][k] != -1)
					k++;
				
				planetLanding[p][k] = player;
			} else {
				if (Integer.parseInt(playerData[B_MISSION].getData()) == 1)
				{
					int u = Integer.parseInt(playerData[B_TARGET].getData());
					int k = 0;
					while (planetLanding[u][k] != -1)
						k++;
					
					planetLanding[u][k] = player;
				}
			}
		}
		
		for (int i = 0; i < planetNumber; i++)
		{
			ArrayList<int[]> gottenRessources = landOnPlanet(planetLanding[i],clientCodes, planets[i]);
			
			for (int player = 0; player < clientCodes.size(); player++)
				for (int res = 0; res < 10; res++)
					newRessources.get(player)[res] += gottenRessources.get(player)[res];
		}
		
		//for (int i = 0; i < guiParent.getPlayers().getSize(); i++)
		//{
		//	ArrayList<int[]> gottenRessources = landOnShip(userLanding[i], clientCodes, i);
			
		//	for (int player = 0; player < clientCodes.size(); player++)
		//		for (int res = 0; res < 10; res++)
		//			newRessources.get(player)[res] += gottenRessources.get(player)[res];
		//}
		
		generatePlanetData(turn);
		
		for (int player = 0; player < clientCodes.size(); player++)
			returnRessources.add(makeServerString(clientCodes.get(player), newRessources.get(player)));
		
		return returnRessources;
	}
	
	private Data[] makeServerString(Data[] clientCode, int[] ressources)
	{
		int type = Integer.parseInt(clientCode[B_RESPONSE_TYPE].getData());
		int total = 0;
		int ressourceTypesAllowed = 3;
		Data[] returnData = null;
		
		if (type == 0)
			returnData = new Data[8];
		else if (type == 1)
			returnData = new Data[11];
		else if (type == 2)
			returnData = new Data[29];
		else if (type == 3)
			returnData = new Data[29];
		else
			returnData = new Data[40];
		
		returnData[0] = new Data(type);
		returnData[1] = new Data(planetNumber);
		total += 2;
		
		if (type > 0)
		{
			for (int i = 0; i < planetNumber;i++)
		        returnData[total+i] = new Data(planets[i].getType());
			
			total += 3;
		}
		
		if (type == 2)
		{
			for (int i = 0; i < planetNumber;i++)
			{
		        returnData[total+i*6] = new Data(planets[i].getRessources()[0] > 0 ? 1 : 0);
		        returnData[total+1+i*6] = new Data(planets[i].getRessources()[1] > 0 ? 1 : 0);
		        returnData[total+2+i*6] = new Data(planets[i].getRessources()[2] > 0 ? 1 : 0);
		        returnData[total+3+i*6] = new Data(planets[i].getRessources()[3] > 0 ? 1 : 0);
		        returnData[total+4+i*6] = new Data(planets[i].getRessources()[4] > 0 ? 1 : 0);
		        returnData[total+5+i*6] = new Data(planets[i].getRessources()[5] > 0 ? 1 : 0);
			}
			
			total += 18;
		} else if (type > 2) {
			for (int i = 0; i < planetNumber;i++)
			{
		        returnData[total+i*6] = new Data(planets[i].getRessources()[0]);
		        returnData[total+1+i*6] = new Data(planets[i].getRessources()[1]);
		        returnData[total+2+i*6] = new Data(planets[i].getRessources()[2]);
		        returnData[total+3+i*6] = new Data(planets[i].getRessources()[3]);
		        returnData[total+4+i*6] = new Data(planets[i].getRessources()[4]);
		        returnData[total+5+i*6] = new Data(planets[i].getRessources()[5]);
			}
			
			total += 18;
		}
		
		if (type > 3)
		{
			for (int i = 0; i < planetNumber;i++)
			{
		        returnData[total+i*3] = new Data(planets[i].getStats()[0]);
		        returnData[total+1+i*3] = new Data(planets[i].getStats()[1]);
		        returnData[total+2+i*3] = new Data(planets[i].getStats()[2]);
			}
			
			total += 9;
			ressourceTypesAllowed = 4;
		}
		
		int currentTotal = 0;
		for (int i = 0; i < 10; i++)
		{
			if (ressources[i] != 0 && currentTotal < ressourceTypesAllowed)
			{
				returnData[total+2*currentTotal] = new Data(i);
				returnData[total+1+2*currentTotal] = new Data(ressources[i]);
				currentTotal++;
			}
		}
		
		for (int i = currentTotal; i < ressourceTypesAllowed;i++)
		{
			returnData[total+2*i] = new Data(0);
			returnData[total+1+2*i] = new Data(0);
		}
		
		return returnData;
	}
	
	private ArrayList<int[]> landOnPlanet(int[] players, ArrayList<Data[]> clientCodes, Planet planet)
	{
		ArrayList<int[]> returnRes = new ArrayList<int[]>();
		
		int[] planetRessources = planet.getRessources();
		
		int[][] playerGains = new int[clientCodes.size()][10];
		
		for (int i = 0; i < clientCodes.size(); i++)
			for (int j = 0; j < 9; j++)
				playerGains[i][j] = 0;
		
		for (int i = 0; i < 9; i++)
		{
			if (planetRessources[i] == 0)
				continue;
			
			int[] conflictingPlayers = new int[clientCodes.size()];
			int conflictSize = 0;
			for (int j = 0; j < clientCodes.size(); j++)
			{
				boolean able = false;
				if (players[j] == -1)
					break;
				
				if (i > 5)
					able = true;
				else
					if ((Integer.parseInt(clientCodes.get(players[j])[B_AVL_RES].getData()) & (1 << i)) != 0)
						able = true;
				
				if (able)
				{
					conflictingPlayers[conflictSize] = players[j];
				    conflictSize++;
				}
			}
			
			if (conflictSize == 0)
				continue;
			
			if (planetRessources[i] == 3)
			{
				for (int j = 0; j < conflictSize; j++)
				{
					playerGains[conflictingPlayers[j]][i] = 1;
				}
				
				continue;
			}
			
			if (planetRessources[i] == 2)
			{
				if (i < 2 || conflictSize > 2)
				{
					for (int j = 0; j < conflictSize; j++)
					{
						playerGains[conflictingPlayers[j]][i] = 1;
					}
					
					continue;
				}
				
				if (doPlanetBattle(conflictingPlayers, conflictSize, clientCodes, planet))
				{
					for (int j = 0; j < conflictSize; j++)
					{
						playerGains[conflictingPlayers[j]][i] = 1;
					}
					
					continue;
				}
				
				continue;
			}
			
			if (planetRessources[i] == 1)
			{
				boolean getStuff = false;
				
				if (i < 2)
				{
				    getStuff = true;	
				}
				
				if (doPlanetBattle(conflictingPlayers,conflictSize,clientCodes, planet))
				{
					getStuff = true;
				}
				
				if (getStuff)
				{
					if (conflictSize == 1)
						playerGains[conflictingPlayers[0]][i] = 1;	
					else
						playerGains[doPlayerBattle(conflictingPlayers,conflictSize,clientCodes)][i] = 1;
				}
				
				continue;
			}
		}
		
		for (int i = 0; i < clientCodes.size(); i++)
		{
			int resKinds = 0;
			
			for (int j = 0; j < 9; j++)
				if (playerGains[i][j] == 1)
					resKinds++;
			
			if (resKinds != 0)
			{
			    int min = Integer.parseInt(clientCodes.get(i)[B_CARRY].getData()) / resKinds;
			    int rest = Integer.parseInt(clientCodes.get(i)[B_CARRY].getData()) - min*resKinds;
			    
			    for (int j = 0; j < 9; j++)
					if (playerGains[i][j] == 1)
					{
						playerGains[i][j] = min + (rest > 0 ? 1 : 0);
						rest--;
					}
			}
		}
		
		for (int i = 0; i < clientCodes.size(); i++)
		{
			returnRes.add(playerGains[i]);
		}
		
		return returnRes;
	}
	
	// TODO: This function 
	/*private ArrayList<int[]> landOnShip(int[] players, ArrayList<Data[]> clientCodes, int ship)
	{
		ArrayList<int[]> returnRes = new ArrayList<int[]>();
		
		int[][] playerGains = new int[clientCodes.size()][9];
		
		return returnRes;
	}*/
	
	private int doPlayerBattle(int[] conflictingPlayers, int playerNum, ArrayList<Data[]> clientCodes)
	{
		if (playerNum > 2)
		{
			int winningPlayer = -1;
			int currentDiff = -99;
			for (int i = 0; i < playerNum; i++)
			{
				int attackSum = 0;
				for (int j = 0; j < playerNum;j++)
					if (j != i)
						attackSum += Integer.parseInt(clientCodes.get(conflictingPlayers[j])[B_POWER].getData());
				
				attackSum /= playerNum-1;
				
				int raw = Integer.parseInt(clientCodes.get(conflictingPlayers[i])[B_DEFENSE].getData()) - attackSum;
				
				if (raw > currentDiff)
				{
					currentDiff = raw;
					winningPlayer = i;
				}
			}
			
			return winningPlayer;
		}
		
		int player1diff = Integer.parseInt(clientCodes.get(conflictingPlayers[0])[B_DEFENSE].getData()) - Integer.parseInt(clientCodes.get(conflictingPlayers[1])[B_POWER].getData());
		int player2diff = Integer.parseInt(clientCodes.get(conflictingPlayers[1])[B_DEFENSE].getData()) - Integer.parseInt(clientCodes.get(conflictingPlayers[0])[B_POWER].getData());
		
		int player1Adv = player1diff - player2diff;
		
	    Random rand = new Random();
	    if ((rand.nextInt(10) + 1 + player1Adv) > 5)
	    	return conflictingPlayers[0];
		
		return conflictingPlayers[1];
	}
	
	private boolean doPlanetBattle(int[] conflictingPlayers, int playerNum, ArrayList<Data[]> clientCodes, Planet planet)
	{
	    int handicap = 5;
	    
	    int groupBrain = 0;
	    int groupPower = 0;
	    int groupDefense = 0;
	    
	    for (int i = 0; i < playerNum; i++)
	    {
	    	groupBrain += Integer.parseInt(clientCodes.get(conflictingPlayers[i])[B_BRAIN].getData());
	    	groupPower += Integer.parseInt(clientCodes.get(conflictingPlayers[i])[B_POWER].getData());
	    	groupDefense += Integer.parseInt(clientCodes.get(conflictingPlayers[i])[B_DEFENSE].getData());
	    }
	    
	    Random rand = new Random();
	    
	    switch (planet.getStats()[2])
	    {
	        case 0:
	        	handicap = groupBrain;
	        case 1:
	        	handicap = (rand.nextInt(2)+2);
	        case 2:
	        	handicap = (rand.nextInt(3)-1);
	        case 3:
	        	handicap = -(rand.nextInt(2)+2);
	    }
	    
	    if ((rand.nextInt(10) + 1 + handicap) > 5)
	        return true;
	        
	    handicap = 0;
	    switch (planet.getStats()[0])
	    {
	        case 0:
	        	handicap = groupPower;
	        case 1:
	        	handicap = (rand.nextInt(2)+2);
	        case 2:
	        	handicap = (rand.nextInt(3)-1);
	        case 3:
	        	handicap = -(rand.nextInt(2)+2);
	    }
	    
	    switch (planet.getStats()[1])
	    {
	        case 0:
	        	handicap += groupDefense;
	        case 1:
	        	handicap += (rand.nextInt(2)+2);
	        case 2:
	        	handicap += (rand.nextInt(3)-1);
	        case 3:
	        	handicap += -(rand.nextInt(2)+2);
	    }
	    
	    if ((rand.nextInt(10) + 1 + handicap) > 5)
	        return true;
	    
	    return false;
	}
}
